package IT09;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

/*
统计每个字符串中字符出现的次数
 */
public class HashMapDemo {
    public static void main(String[] args) {
        //输入字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String line = sc.nextLine();
        HashMap<Character,Integer> hm = new HashMap<>();
        //获取每个字符
        for(int i = 0;i < line.length();i++){
            char key = line.charAt(i);
            Integer value = hm.get(key);
            if(value == null){
                hm.put(key,1);
            }else {
                value++;
                hm.put(key,value);
            }
        }
        //遍历HashMap集合，得到键和值，按照要求进行拼接
        StringBuilder sb = new StringBuilder();
        Set<Character> keySet = hm.keySet();
        for(Character key:keySet){
            Integer value = hm.get(keySet);
            sb.append(key).append("(").append(value).append(")");
        }
        String result = sb.toString();
        System.out.println(result);
    }
}
